package me.yricky.alia.ui.layoutPage.renderModel

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Matrix
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.withTransform
import me.yricky.alia.data.hierarchy.Layer

/**
 * @author Yricky
 * @date 2022/2/13
 */

class RenderNode(
    var rotate:Float,
    var scale:Float,
    var offset:Offset,
    var contentOfThisNode:Layer?,
    val childNodes:ArrayList<RenderNode>
){
    private val matrix:Matrix = Matrix()
    private fun getMatrix():Matrix{
        matrix.reset()
        matrix.scale(scale)
        matrix.rotateZ(rotate)
        matrix.translate(offset.x,offset.y)
        return matrix
    }

    fun render(
        renderFunction:DrawScope.(Layer)->Unit
    ):DrawScope.()->Unit{
        return {
            synchronized(this@RenderNode){
                withTransform({ getMatrix() }){
                    childNodes.forEach{ it.render(renderFunction)(this) }
                    contentOfThisNode?.let {
                        renderFunction(this,it)
                    }
                }
            }
        }
    }
}